package lcy.homework01;

import java.util.Objects;

/**
 * @ClassName: Rectangle
 * @Description:
 * @author: 李春洋
 * @date: 2024/3/30 16:27
 * @Blog: https://
 */
public class Rectangle extends Shape {
    Point[] vertices;

    Rectangle(Point[] vertices) {
        this.vertices = vertices;
    }

    @Override
    double calculatePerimeter() {
        double perimeter = 0;
        for (int i = 0; i < 3; i++) {
            perimeter += distance(vertices[i], vertices[i + 1]);
        }
        perimeter += distance(vertices[3], vertices[0]);
        return perimeter;
    }

    @Override
    double calculateArea() {
        return Math.abs((vertices[0].x - vertices[1].x) * (vertices[0].y - vertices[3].y));
    }

    @Override
    void move(Point p) {
        for (Point vertex : vertices) {
            vertex.x += p.x;
            vertex.y += p.y;
        }
    }

    private double distance(Point p1, Point p2) {
        return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Rectangle)) return false;
        Rectangle other = (Rectangle) obj;
        return vertices[0].equals(other.vertices[0]) && vertices[1].equals(other.vertices[1]) &&
                vertices[2].equals(other.vertices[2]) && vertices[3].equals(other.vertices[3]);
    }

    @Override
    public int hashCode() {
        return Objects.hash(vertices[0], vertices[1], vertices[2], vertices[3]);
    }

    @Override
    public String toString() {
        return "长方形 [顶点=" + vertices[0] + ", " + vertices[1] + ", " + vertices[2] + ", " + vertices[3] + "]";
    }
}
